#ifndef CPU_CHECKPORT_H
#define CPU_CHECKPORT_H

#include "data.h"
#include "externals.h"

void CPU_CheckPort(struct Agent_Var* agent, int dir)
{
    agent->condition = 0;
    port_t pid = READ_CODE(agent,port_t);
    pos_t dataPos = READ_CODE(agent,pos_t);
    struct DataEntry* pde1 = getDataEntry(dataPos);
    struct DataEntry* pde2;

    struct Port_Static* ps1 = &tavm_ports_s[pid];
    struct Port_Static* ps2;
    struct Port_Var* pv2;

    int state;
    if(dir == PDIR_IN)
    {
       state = PS_WWD;
    }
    else
    {

        state = PS_WFD;
    }

    port_t* cons = (port_t*)(ps1->portsPos + tavm_tables[TID_PORTS_S].start);

    port_t i;
    for(i = 0; i < ps1->cCount; i++)
    {
        if(cons[i] < 128)
        {
            ps2 = &tavm_ports_s[cons[i]];
            pv2 = &tavm_ports_v[cons[i]];

            if(ps2->dir != dir)
            {
                if(pv2->state == state)
                {
                    pde2 = getDataEntry(pv2->dataPos);
                    if(pde1->type == pde2->type)
                    {
                        agent->condition = CM_READY;
                        break;
                    }
                }
            }
        }
        else
        {
            agent->condition = CM_READY;
            break;
        }
    }
}

#endif


